package com.neusoft.crm.service.impl;

import com.neusoft.crm.entity.CommonResult;
import com.neusoft.crm.entity.Sysinfo;
import com.neusoft.crm.mapper.SysinfoMapper;
import com.neusoft.crm.service.ISysinfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 东软集团
 * @since 2023-04-03
 */
@Service
public class SysinfoServiceImpl extends ServiceImpl<SysinfoMapper, Sysinfo> implements ISysinfoService {

    @Autowired
    SysinfoMapper sysinfoMapper;


    public CommonResult  tree(){
//        获得所有的节点
        List<Sysinfo> lss = sysinfoMapper.selectList(null);
//        寻找根节点
        Sysinfo root = null;
        for ( Sysinfo  info : lss             ) {
            if (info.getPresysno() !=null && info.getPresysno().intValue() ==0 ){
                root = info;
            }
        }
//        为根节点填充孩子
        //        为孩子节点填充孩子节点
        fillTree(root, lss  );
        Sysinfo [] sysinfos =  { root };
        CommonResult  result =new CommonResult( sysinfos);
        return result;

    }

    private void fillTree(Sysinfo root, List<Sysinfo> lss) {
        if (  root ==null ) return;
        for (int i = 0; root !=null &&  lss!=null &&  i < lss.size(); i++) {
            Sysinfo node =  lss.get(i);
            if (node.getPresysno().intValue() ==  root.getSysno().intValue() ){
                root.getChildren().add(node);
            }
        }
        for (int i = 0;  root.getChildren()!=null  &&   root.getChildren().size()>0  &&   i <  root.getChildren().size()  ; i++) {
            Sysinfo node =   root.getChildren().get(i);
            fillTree( node, lss  );
        }
    }
}
